package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.ServiceEntity;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author : younghow
 * @date : 2021/7/714:50
 * @desc :
 */
public class LaborServiceAndEmploymentPublishViewModel extends AndroidViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> mUploadSuccessData = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mCityData = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mWorkTypeData = new MutableLiveData<>();
    private List<PickSelectEntity> mServiceList = new ArrayList<>();
    public MutableLiveData<List<PickSelectEntity>> mPickSelectData = new MutableLiveData<>();
    private List<PickSelectEntity> listFs;
    private List<PickSelectEntity> mApplyListFs;

    public LaborServiceAndEmploymentPublishViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void upload(Map<String, Object> map) {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().uploadLabour(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mUploadSuccessData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getLinkAgeWorkType() {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().getLinkAgeWorkType2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mWorkTypeData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getCityList() {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        for (int i = 0; i < response.body().getData().size(); i++) {
                            if (response.body().getData().get(i) == null) {
                                return;
                            }
                            mCityData.postValue(response.body().getData());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getCustomerData() {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getMachineAPI().getCustomerOrg().enqueue(new Callback<BaseEntity<List<ServiceEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ServiceEntity>>> call, Response<BaseEntity<List<ServiceEntity>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    List<ServiceEntity> data = response.body().getData();
                    if (data == null || data.size() == 0) {
                        return;
                    }
                    for (int i = 0; i < data.size(); i++) {
                        PickSelectEntity pickSelectEntity = new PickSelectEntity();
                        pickSelectEntity.setNo(data.get(i).getId());
                        pickSelectEntity.setProjectscale(data.get(i).getName());
                        mServiceList.add(pickSelectEntity);
                    }
                    mPickSelectData.postValue(mServiceList);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ServiceEntity>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public List<PickSelectEntity> getSalaryFs() {
        if (listFs == null) {
            listFs = new ArrayList<>();
            PickSelectEntity entity1 = new PickSelectEntity();
            entity1.setNo("1");
            entity1.setProjectscale("自定义薪资");
            PickSelectEntity entity2 = new PickSelectEntity();
            entity2.setNo("2");
            entity2.setProjectscale("电议");
            PickSelectEntity entity3 = new PickSelectEntity();
            entity3.setNo("3");
            entity3.setProjectscale("面议");
            listFs.add(entity1);
            listFs.add(entity3);
            listFs.add(entity2);
        }
        return listFs;
    }

    public List<PickSelectEntity> getApplyFs() {
        if (mApplyListFs == null) {
            mApplyListFs = new ArrayList<>();
            PickSelectEntity entity1 = new PickSelectEntity();
            entity1.setNo("1");
            entity1.setProjectscale("启用");
            PickSelectEntity entity2 = new PickSelectEntity();
            entity2.setNo("0");
            entity2.setProjectscale("禁用");
            mApplyListFs.add(entity1);
            mApplyListFs.add(entity2);
        }
        return mApplyListFs;
    }
}
